lunomods.fileVariables = minetest.get_worldpath().."/variables.tbl"
lunomods.variables={  global={}, players={} 	}
lunomods.doOpen = function()
	local file = io.open(lunomods.fileVariables, "r")
	if file then
		local table = minetest.deserialize(file:read("*all"))
		file:close()
		if type(table) == "table" then
			lunomods.variables = table
			return
		end
		minetest.log('action',"[LUNOMODS] doOpen("..lunomods.fileVariables.."')")
		if minetest.setting_getbool("log_mods") then print("doOpen("..lunomods.fileVariables.."')") end
	end
end
lunomods.doSave = function()
	--file = io.open(lunomods.fileVariables,"a+")
	local file = io.open(lunomods.fileVariables,"w")
	if file then
		file:write(minetest.serialize(lunomods.variables))
		file:close()
	end
	minetest.log('action',"[LUNOMODS] doSave("..lunomods.fileVariables.."')")
	--if minetest.setting_getbool("log_mods") then print("doSave("..lunomods.fileVariables.."')") end
end
lunomods.setGlobalValue = function(variavel, valor)
	if lunomods.variables.global==nil then
		lunomods.variables.global = {}
	end
	if valor~=nil or lunomods.variables.global[variavel]~=nil then --Verifica se nao ja estava apagada
		if type(valor)=="number" then valor = valor * 1 end --para transformar em numero
		lunomods.variables.global[variavel] = valor
		if minetest.setting_getbool("log_mods") then
			if valor~= nil then
				print("lunomods.setGlobalValue("..variavel.."='"..valor.."')")
			else
				print("lunomods.setGlobalValue("..variavel.."=nil)")
			end
		end
	end
	--lunomods.doSave() --Salva quando desliga o server ou quando sai o jogador
end
lunomods.getGlobalValue = function(variavel)
	if lunomods.variables.global~=nil and lunomods.variables.global[variavel]~=nil then
		local valor = lunomods.variables.global[variavel]
		if minetest.setting_getbool("log_mods") then
			print("lunomods.getGlobalValue("..variavel..") = '"..valor.."'")
		end
		return valor
	end
	return nil
end
lunomods.setCharValue = function(charName, variavel, valor)
	if lunomods.variables.players[charName] == nil then
		lunomods.variables.players[charName] = {}
	end
	if valor~=nil or lunomods.variables.players[charName][variavel]~=nil then --Verifica se nao ja estava apagada
		if type(valor)=="number" then valor = valor * 1 end --para transformar em numero
		lunomods.variables.players[charName][variavel] = valor
		if minetest.setting_getbool("log_mods") then
			if valor~= nil then
				print("lunomods.setCharValue("..charName..":"..variavel.."='"..valor.."')")
			else
				print("lunomods.setCharValue("..charName..":"..variavel.."=nil)")
			end
		end
	end
	--lunomods.doSave() --Salva quando desliga o server ou quando sai o jogador
end

lunomods.getCharValue = function(charName, variavel)
	if lunomods.variables.players[charName]~=nil and lunomods.variables.players[charName][variavel]~=nil then
		local valor = lunomods.variables.players[charName][variavel]
		if minetest.setting_getbool("log_mods") then
			print("lunomods.getCharValue("..charName..":"..variavel..") = '"..valor.."'")
		end
		return valor
	end
	return nil
end

lunomods.doOpen()

--##########################################################################################################

if not minetest.setting_getbool("log_mods") then 
	minetest.setting_set("log_mods","false")
end

minetest.register_on_joinplayer(function(player)
	lunomods.doSave()
end)

minetest.register_on_leaveplayer(function(player)
	lunomods.doSave()
end)

minetest.register_on_shutdown(function()
	lunomods.doSave()
end)

--##########################################################################################################

--[[
lunomods = {
	fileVariables = minetest.get_worldpath().."/variables.tbl",
	variables={  global={}, players={} 	},
	doOpen = function()
		local file = io.open(lunomods.fileVariables, "r")
		if file then
			local table = minetest.deserialize(file:read("*all"))
			file:close()
			if type(table) == "table" then
				lunomods.variables = table
				return
			end
			if minetest.setting_getbool("log_mods") then
				print("doOpen("..lunomods.fileVariables.."')")
			end
		end
	end,
	doSave = function()
		--file = io.open(lunomods.fileVariables,"a+")
		file = io.open(lunomods.fileVariables,"w")
		if file then
			file:write(minetest.serialize(lunomods.variables))
			file:close()
		end
		if minetest.setting_getbool("log_mods") then
			print("doSave("..lunomods.fileVariables.."')")
		end
	end,
	lunomods.setGlobalValue = function(variavel, valor)
		if lunomods.variables.global==nil then
			lunomods.variables.global = {}
		end
		if valor~=nil or lunomods.variables.global[variavel]~=nil then --Verifica se nao ja estava apagada
			lunomods.variables.global[variavel] = valor
			if minetest.setting_getbool("log_mods") then
				if valor~= nil then
					print("lunomods.setGlobalValue("..variavel.."='"..valor.."')")
				else
					print("lunomods.setGlobalValue("..variavel.."=nil)")
				end
			end
		end
		--lunomods.doSave() --Salva quando desliga o server ou quando sai o jogador
	end,
	lunomods.getGlobalValue = function(variavel)
		if lunomods.variables.global~=nil and lunomods.variables.global[variavel]~=nil then
			local valor = lunomods.variables.global[variavel]
			if minetest.setting_getbool("log_mods") then
				print("lunomods.getGlobalValue("..variavel..") = '"..valor.."'")
			end
			return valor
		end
		return nil
	end,
	lunomods.setCharValue = function(charName, variavel, valor)
		if lunomods.variables.players[charName]==nil then
			lunomods.variables.players[charName] = {}
		end
		if valor~=nil or lunomods.variables.players[charName][variavel]~=nil then --Verifica se nao ja estava apagada
			lunomods.variables.players[charName][variavel] = valor
			if minetest.setting_getbool("log_mods") then
				if valor~= nil then
					print("lunomods.setCharValue("..charName..":"..variavel.."='"..valor.."')")
				else
					print("lunomods.setCharValue("..charName..":"..variavel.."=nil)")
				end
			end
		end
		--lunomods.doSave() --Salva quando desliga o server ou quando sai o jogador
	end,
	lunomods.getCharValue = function(charName, variavel)
		if lunomods.variables.players[charName]~=nil and lunomods.variables.players[charName][variavel]~=nil then
			local valor = lunomods.variables.players[charName][variavel]
			if minetest.setting_getbool("log_mods") then
				print("lunomods.getCharValue("..charName..":"..variavel..") = '"..valor.."'")
			end
			return valor
		end
		return nil
	end,
}
]]--

--##########################################################################################################

minetest.register_chatcommand("set_global_value", {
	params = "<variavel> <valor>",
	description = "Grava uma variavel no mapa (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		-- Returns (pos, true) if found, otherwise (pos, false)
		--variavel, valor = string.match(param, "^([^ ]+) +([^ ]+)")
		local variavel, valor = string.match(param, "([%a%d_]+) (.+)")
		if variavel~=nil and variavel~="" and valor~=nil and valor~="" then
			lunomods.setGlobalValue(variavel, valor)
			minetest.chat_send_player(player, variavel.."='"..valor.."'")
		else
			minetest.chat_send_player(player, "SINTAXE: /set_global_value <variavel> <valor>")
		end
		return
	end,
})

minetest.register_chatcommand("del_global_value", {
	params = "<variavel>",
	description = "Apaga uma variavel no mapa (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		local variavel = string.match(param, "([%a%d_]+)")
		if variavel~=nil and variavel~="" then
			lunomods.setGlobalValue(variavel, nil)
			minetest.chat_send_player(player, "Variavel '"..variavel.."' apagada!")
		else
			minetest.chat_send_player(player, "SINTAXE: /del_global_value <variavel>")
		end
		return
	end,
})

minetest.register_chatcommand("get_global_value", {
	params = "<variavel>",
	description = "Exibe o valor de uma variavel do mapa (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		local variavel = string.match(param, "([%a%d_]+)")
		if variavel~=nil and variavel~="" then
			local valor = lunomods.getGlobalValue(variavel)
			if valor~=nil then
				minetest.chat_send_player(player, variavel.."="..dump(valor))
			else
				minetest.chat_send_player(player, "Variavel '"..variavel.."' nao foi definida.")
			end
		else
			minetest.chat_send_player(player, "SINTAXE: /get_global_value <variavel>")
		end
		return
	end,
})

minetest.register_chatcommand("set_char_value", {
	params = "<jogador> <variavel> <valor>",
	description = "Grava uma variavel no jogador indicado. (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		-- Returns (pos, true) if found, otherwise (pos, false)
		--variavel, valor = string.match(param, "^([^ ]+) +([^ ]+)")
		local charName, variavel, valor = string.match(param, "([%a%d_]+) ([%a%d_]+) (.+)")
		if charName~=nil and charName~="" and variavel~=nil and variavel~="" and valor~=nil and valor~="" then
			lunomods.setCharValue(charName, variavel, valor)
			minetest.chat_send_player(player, charName.."."..variavel.."='"..valor.."'")
		else
			minetest.chat_send_player(player, "SINTAXE: /set_char_value <jogador> <variavel> <valor>")
		end
		return
	end,
})

minetest.register_chatcommand("del_char_value", {
	params = "<jogador> <variavel>",
	description = "Apaga uma variavel no jogador indicado. (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		local charName, variavel = string.match(param, "([%a%d_]+) ([%a%d_]+)")
		if charName~=nil and charName~="" and variavel~=nil and variavel~="" then
			lunomods.setCharValue(charName, variavel, nil)
			minetest.chat_send_player(player, charName.."."..variavel.."=nil")
		else
			minetest.chat_send_player(player, "SINTAXE: /del_char_value <jogador> <variavel>")
		end
		return
	end,
})

minetest.register_chatcommand("get_char_value", {
	params = "<jogador> <variavel>",
	description = "Exibe o valor de uma variavel no jogador indicado. (Necessita de privilegio 'server').",
	privs = {server=true},
	func = function(player, param)
		local charName, variavel = string.match(param, "([%a%d_]+) ([%a%d_]+)")
		if charName~=nil and charName~="" and variavel~=nil and variavel~="" then
			local valor = lunomods.getCharValue(charName, variavel)
			if valor~=nil then
				minetest.chat_send_player(player, charName.."."..variavel.."="..dump(valor))
			else
				minetest.chat_send_player(player, "Variavel '"..charName.."."..variavel.."' nao foi definida.")
			end
		else
			minetest.chat_send_player(player, "SINTAXE: /get_char_value <jogador> <variavel>")
		end
		return
	end,
})

